﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TransCend.Util.Page
{
    public class SimplePage : IPage
    {
        /**
	 * 默认分页大小
	 */
       // public static readonly int DEF_COUNT = SinglePage.ERF_Page;
        public static readonly int DEF_COUNT = Resources.GetResources().GetInt("trans.pagesize");

        private int totalCount = 0;
        private int pageSize = DEF_COUNT;
        private int pageNo = 1;

        public SimplePage()
        {

        }

        public SimplePage(int pageNo, int pageSize, int totalCount)
        {
            if (totalCount <= 0)
            {
                this.totalCount = 0;
            }
            else
            {
                this.totalCount = totalCount;
            }
            if (pageSize <= 0)
            {
                this.pageSize = DEF_COUNT;
            }
            else
            {
                this.pageSize = pageSize;
            }
            if (pageNo <= 0)
            {
                this.pageNo = 1;
            }
            else
            {
                this.pageNo = pageNo;
            }

        }

        /**
         * 调整分页参数，使合理化
         */
        public void AdjustPage()
        {
            if (totalCount <= 0)
            {
                totalCount = 0;
            }
            if (pageSize <= 0)
            {
                pageSize = DEF_COUNT;
            }
            if (pageNo <= 0)
            {
                pageNo = 1;
            }
            if ((pageNo - 1) * pageSize >= totalCount)
            {
                pageNo = totalCount / pageSize;
            }
        }

        public int GetPageNo()
        {
            return pageNo;
        }

        public int GetPageSize()
        {
            return pageSize;
        }

        public int GetTotalCount()
        {
            return totalCount;
        }

        public int GetTotalPage()
        {
            int totalPage = totalCount / pageSize;
            if (totalCount % pageSize != 0 || totalPage == 0)
            {
                totalPage++;
            }
            return totalPage;
        }

        public bool IsFirstPage()
        {
            return pageNo <= 1;
        }

        public bool IsLastPage()
        {
            return pageNo >= GetTotalPage();
        }

        public int GetNextPage()
        {
            if (IsLastPage())
            {
                return pageNo;
            }
            else
            {
                return pageNo + 1;
            }
        }

        public int GetPrePage()
        {
            if (IsFirstPage())
            {
                return pageNo;
            }
            else
            {
                return pageNo - 1;
            }
        }

        public void SetTotalCount(int totalCount)
        {
            this.totalCount = totalCount;
        }

        public void SetPageSize(int pageSize)
        {
            this.pageSize = pageSize;
        }

        public void SetPageNo(int pageNo)
        {
            this.pageNo = pageNo;
        }
    }
}
